package com.ojoin.trade.supplyloan_core.web.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.common.web.service.impl.BaseServiceImpl;
import com.ojoin.trade.supplyloan_core.web.dao.TbSlSupplyPaymentDao;
import com.ojoin.trade.supplyloan_core.web.domain.TbCatchdataStrategy;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyPayment;
import com.ojoin.trade.supplyloan_core.web.service.TbCatchdataStrategyService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlPlatformUserService;
import com.ojoin.trade.supplyloan_core.web.service.TbSlSupplyPaymentService;

@Service
public class TbSlSupplyPaymentServiceImpl extends
		BaseServiceImpl<TbSlSupplyPayment> implements TbSlSupplyPaymentService {

	@Autowired
	private TbSlSupplyPaymentDao dao;
	
	@Autowired
	private TbSlPlatformUserService platformUserService;
	
	@Autowired
	private TbCatchdataStrategyService catchdataStrategyService;

	private Logger log = LoggerFactory
			.getLogger(TbSlSupplyPaymentServiceImpl.class);

	@Override
	protected TbSlSupplyPaymentDao getBaseDao() {
		return dao;
	}

	@Override
	public int deleteByPlatformAccountId(Integer platformAccountId) {
		return deleteByPlatformAccountId(platformAccountId, null);
	}

	@Override
	public int deleteByPlatformAccountId(Integer platformAccountId,
			String rowCreateTime) {
		return getBaseDao().deleteByPlatformAccountId(platformAccountId,
				rowCreateTime);
	}

	@Override
	public List<TbSlSupplyPayment> queryByUidAndCreateDate(
			int platformAccountId, String createDateStart,
			String createDateEnd, String settlementState, Pageable page) {
		return this.getBaseDao().queryByUidAndCreateDate(platformAccountId,
				createDateStart, createDateEnd, settlementState, page);
	}

	@Override
	public List<TbSlSupplyPayment> queryByUidAndCreateDate(
			int platformAccountId, String createDateStart,
			String createDateEnd, String settlementState) {
		return queryByUidAndCreateDate(platformAccountId, createDateStart,
				createDateEnd, settlementState, null);
	}

	@Override
	public List<TbSlSupplyPayment> queryByUidAndCreateDate(
			int platformAccountId, String createDateStart, String createDateEnd) {
		return queryByUidAndCreateDate(platformAccountId, createDateStart,
				createDateEnd, null);
	}

	@Override
	public List<TbSlSupplyPayment> queryByUidAndRowCreate(
			int platformAccountId, String rowCreateTimeStart,
			String rowCreateTimeEnd, String settlementState, Pageable page) {
		return this.getBaseDao().queryByUidAndRowCreate(platformAccountId,
				rowCreateTimeStart, rowCreateTimeEnd, settlementState, page);
	}

	@Override
	public List<TbSlSupplyPayment> queryByUidAndRowCreate(
			int platformAccountId, String rowCreateTimeStart,
			String rowCreateTimeEnd, String settlementState) {
		return queryByUidAndRowCreate(platformAccountId, rowCreateTimeStart,
				rowCreateTimeEnd, settlementState, null);
	}

	@Override
	public List<TbSlSupplyPayment> queryByUidAndRowCreate(
			int platformAccountId, String rowCreateTimeStart,
			String rowCreateTimeEnd) {
		return queryByUidAndRowCreate(platformAccountId, rowCreateTimeStart,
				rowCreateTimeEnd, null);
	}

	@Override
	public Map<String, TbSlSupplyPayment> queryByUidAndCreateDateMap(
			int platformAccountId, String createDateStart,
			String createDateEnd, String settlementState) {

		Map<String, TbSlSupplyPayment> map = null;
		List<TbSlSupplyPayment> li = queryByUidAndCreateDate(platformAccountId,
				createDateStart, createDateEnd, settlementState);
		if (null != li && li.size() > 0) {
			map = new HashMap<String, TbSlSupplyPayment>();
			for (int i = 0; i < li.size(); i++) {
				TbSlSupplyPayment tbsl = li.get(i);
				if (CommonUtils.isEmpty(settlementState)) {
					throw new BusinessException(
							"settlementState can not be null");
				} else if (settlementState.equals(TbSlSupplyPayment.WAITPAY)) {
					map.put(tbsl.getSettlementId(), tbsl);
				} else if (settlementState
						.equals(TbSlSupplyPayment.COMPLETEPAY)) {
					map.put(tbsl.getPayId(), tbsl);
				}

			}
		}

		return map;
	}

	@Override
	public long querySupplyPaymentMoney(int platformAccountId,
			String createDateStart, String createDateEnd) {
		return this.getBaseDao().querySupplyPaymentMoney(platformAccountId,
				createDateStart, createDateEnd);
	}

	@Override
	public long querySupplysettlementMoneyByCreateDate(int platformAccountId,
			String createDateStart, String createDateEnd) {
		return querySupplysettlementMoney(platformAccountId, createDateStart,
				createDateEnd, null, null, null);
	}

	@Override
	public long querySupplysettlementMoneyByRowCreateTime(
			int platformAccountId, String rowCreateTimeStart,
			String rowCreateTimeEnd) {
		return querySupplysettlementMoney(platformAccountId, null, null,
				rowCreateTimeStart, rowCreateTimeEnd, null);
	}

	@Override
	public long querySupplysettlementMoney(int platformAccountId,
			String createDateStart, String createDateEnd,
			String rowCreateTimeStart, String rowCreateTimeEnd,
			String settlementState) {
		return this.getBaseDao().querySupplysettlementMoney(platformAccountId,
				createDateStart, createDateEnd, rowCreateTimeStart,
				rowCreateTimeEnd, settlementState);
	}

	@Override
	public long querySupplysettlementMoney(int platformAccountId) {
		return querySupplysettlementMoney(platformAccountId, null, null, null,
				null, null);
	}

	@Override
	public long querySupplysettlementMoney(int platformAccountId,
			String settlementState) {
		return querySupplysettlementMoney(platformAccountId, null, null, null,
				null, settlementState);
	}

	@Override
	public long querySupplysettlementMoneyByRowCreateTimeToday(
			int platformAccountId) {
		return querySupplysettlementMoneyByRowCreateTime(platformAccountId,
				TimeUtils.getCurDate(), TimeUtils.getCurDate());
	}

	@Override
	public Map<String, TbSlSupplyPayment> queryByUidAndCreateDateMapToday(
			int platformAccountId, String settlementState) {
		return queryByUidAndCreateDateMap(platformAccountId,
				TimeUtils.getCurDate(), TimeUtils.getCurDate(), settlementState);
	}

	@Override
	public void deleteRepeat(int platformAccountId) {
		this.getBaseDao().deleteRepeat(platformAccountId);

	}

	@Override
	public Map<String, TbSlSupplyPayment> queryByUidAndsettlementState(
			int platformAccountId, String settlementState) {
		return queryByUidAndCreateDateMap(platformAccountId, null, null,
				settlementState);
	}

	@Override
	public String getLastInsertTime(int platformAccountId) {
		String time = dao.getLastInsertTime(platformAccountId);
		if (!CommonUtils.isEmpty(time)) {
			time = time.substring(0, 10);
		}
		return time;
	}

	@Override
	public void removeSettlementCommon(List<TbSlSupplyPayment> list,
			Map<String, TbSlSupplyPayment> MapDB) {

		if (!CommonUtils.isEmpty(MapDB)) {
			Set<String> set = MapDB.keySet();

			if (!CommonUtils.isEmpty(list)) {
				LogUtils.info(log, "结算单据:" + list.size());
				Iterator<TbSlSupplyPayment> it = list.iterator();
				while (it.hasNext()) {
					TbSlSupplyPayment payment = it.next();
					if (set.contains(payment.getSettlementId())) {

						it.remove();

						MapDB.remove(payment.getSettlementId());

						LogUtils.info(log, "结算单据:" + payment.getSettlementId()
								+ "已存在");

					} else {
						LogUtils.info(log, "结算单据:" + payment.getSettlementId()
								+ "不存在");
					}
				}
			}
		} else {
			if (!CommonUtils.isEmpty(list)) {
				LogUtils.info(log, "结算单据:" + list.size());
				Iterator<TbSlSupplyPayment> it = list.iterator();
				while (it.hasNext()) {
					TbSlSupplyPayment payment = it.next();
					LogUtils.info(log, "结算单据:" + payment.getSettlementId()
							+ "不存在");
				}
			}
		}

	}

	@Override
	public void removePaymentCommon(List<TbSlSupplyPayment> list,
			Map<String, TbSlSupplyPayment> MapDB) {
		if (!CommonUtils.isEmpty(MapDB)) {
			Set<String> set = MapDB.keySet();

			if (!CommonUtils.isEmpty(list)) {
				LogUtils.info(log, "付款单据:" + list.size());

				Iterator<TbSlSupplyPayment> it = list.iterator();
				while (it.hasNext()) {
					TbSlSupplyPayment payment = it.next();
					if (set.contains(payment.getPayId())) {

						it.remove();

						MapDB.remove(payment.getPayId());

						LogUtils.info(log, "付款单据:" + payment.getPayId() + "已存在");

					} else {
						LogUtils.info(log, "付款单据:" + payment.getSettlementId()
								+ "不存在");
					}
				}
			}
		} else {
			if (!CommonUtils.isEmpty(list)) {
				LogUtils.info(log, "付款单据:" + list.size());

				Iterator<TbSlSupplyPayment> it = list.iterator();
				while (it.hasNext()) {
					TbSlSupplyPayment payment = it.next();

					LogUtils.info(log, "付款单据:" + payment.getPayId() + "不存在");

				}
			}
		}

	}

	@Override
	public List<TbSlSupplyPayment> changeToPay(
			Map<String, TbSlSupplyPayment> MapDB) {
		if (!CommonUtils.isEmpty(MapDB)) {
			Collection<TbSlSupplyPayment> co = MapDB.values();
			Iterator<TbSlSupplyPayment> it = co.iterator();
			while (it.hasNext()) {
				TbSlSupplyPayment tbsl = it.next();

				tbsl.setSettlementState(TbSlSupplyPayment.COMPLETEPAY);
				tbsl.setPayDate(TimeUtils.getCurDate());

				LogUtils.info(log, "结算单据:" + tbsl.getSettlementId()
						+ "不存在与结算单，已付款，状态更新");
			}

			return new ArrayList<TbSlSupplyPayment>(MapDB.values());
		}

		return null;
	}

	@Override
	public String getLastsettlementDate(int platformAccountId) {
		return dao.getLastsettlementDate(platformAccountId);
	}

	@Override
	public String getLastcreateDate(int platformAccountId) {
		return getLastcreateDate(platformAccountId, null);
	}

	@Override
	public String getLastcreateDate(int platformAccountId,
			String settlementState) {
		return dao.getLastcreateDate(platformAccountId, settlementState);
	}

	@Override
	public List<TbSlSupplyPayment> queryByUidAndCreateDateAndRowCreate(
			int platformAccountId, String createDateStart,
			String createDateEnd, String rowCreateTimeStart,
			String rowCreateTimeEnd, String settlementState, Pageable page) {
		return dao.queryByUidAndRowCreateAndCreateDate(platformAccountId,
				rowCreateTimeStart, rowCreateTimeEnd, createDateStart,
				createDateEnd, settlementState, page);
	}

	@Override
	public List<TbSlSupplyPayment> queryByUidAndRowUpdate(
			int platformAccountId, String rowUpdateTimeStart,
			String rowUpdateTimeEnd, String settlementState, Pageable page) {
		return dao.queryByUidAndRowUpdate(platformAccountId, rowUpdateTimeStart, rowUpdateTimeEnd, settlementState, page);
	}
 
	@Override 
	public Map<Integer,List<TbSlSupplyPayment>> queryByMerchantUser(int idOfTbMerchantUser,
			String DateStart, String DateEnd,String settlementState) {
		// TODO Auto-generated method stub
		Map<Integer,List<TbSlSupplyPayment>> payments = new HashMap<Integer,List<TbSlSupplyPayment>>();
		List<TbSlPlatformUser> users = platformUserService.queryTbSlPlatformUserByJoinid(idOfTbMerchantUser);
		
		if(!CommonUtils.isEmpty(users)){
			Iterator<TbSlPlatformUser> it =  users.iterator();
			TbSlPlatformUser user =it.next();
			TbCatchdataStrategy ca = catchdataStrategyService.queryByPlatformUserId(user.getId());
			
			
			if(ca.getPaymentCatchAllOrPart().equals(TbCatchdataStrategy.CatchAll)){
				String time = TimeUtils.getCurDate();
				List<TbSlSupplyPayment> li = this.queryByUidAndCreateDateAndRowCreate(user.getId(), DateStart, DateEnd, time, time, settlementState, null);
			    payments.put(user.getId(), li);
			}else {
				List<TbSlSupplyPayment> li = this.queryByUidAndCreateDate(user.getId(), DateStart, DateEnd, settlementState);
				  payments.put(user.getId(), li);
			}
		}

		return payments;
	}

}
